package com.league.zjxn.main

import android.content.Context
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.listener.CustomListener
import com.bigkoo.pickerview.view.OptionsPickerView
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ConvertUtils
import com.league.zjxn.R
import com.league.zjxn.entity.Projects
import com.league.zjxn.jetpack.vmodel.ProjectViewModel
import com.league.zjxn.main.adapter.ProjectGroupAdapter
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeFragment
import kotlinx.android.synthetic.main.fragment_main_project.*

class MainProjectFragment : OwnerViewModeFragment<ProjectViewModel>() {

    companion object {
        fun newInstance(): MainProjectFragment {
            return MainProjectFragment()
        }
    }

    private val mProjectGroupAdapter = ProjectGroupAdapter()

    private var mProvinceId: String? = null
    private var mCityId: String? = null
    private val mOptionsRegion: OptionsPickerView<String> by lazy {
        OptionsPickerBuilder(requireContext()) { options1, option2, options3, v ->
            val regionList = viewModel.regionListLiveData.value
                ?.data?.data ?: return@OptionsPickerBuilder
            val province = regionList[options1]
            mProvinceId = province.id
            val city = province.children
            mCityId = if (city.isNullOrEmpty())
                null else city[option2].id
            tvMainProjectArea.text = if (city.isNullOrEmpty())
                province.name else "${province.name}-${city[option2].name}"

            viewModel.projectList(
                province_id = mProvinceId, city_id = mCityId,
                title = etMainProjectSearchContent.text.toString()
            )

        }.setLayoutRes(R.layout.popup_picker_options) {
            it.findViewById<TextView>(R.id.tv_submit).setOnClickListener {
                mOptionsRegion.returnData()
                mOptionsRegion.dismiss()
            }
            it.findViewById<TextView>(R.id.tv_reset).setOnClickListener {
                mOptionsRegion.dismiss()
                mProvinceId = null
                mCityId = null
                tvMainProjectArea.text = "所在地区"
                viewModel.projectList(
                    province_id = mProvinceId, city_id = mCityId,
                    title = etMainProjectSearchContent.text.toString()
                )
            }
            it.findViewById<TextView>(R.id.tv_cancel).setOnClickListener {
                mOptionsRegion.dismiss()
            }
        }.build()
    }

    override fun viewModel(): ProjectViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.fragment_main_project

    override fun initView() {
        BarUtils.addMarginTopEqualStatusBarHeight(flMainProjectBar)

        rvMainProjectBody.adapter = mProjectGroupAdapter.apply {
            val view = View(requireContext()).apply {
                layoutParams = FrameLayout.LayoutParams(
                    FrameLayout.LayoutParams.MATCH_PARENT,
                    ConvertUtils.dp2px(50f)
                )
            }
            setFooterView(view)
        }

        tvMainProjectArea.setOnClickListener {
            viewModel.userRegionList()
        }

        tvMainProjectSearchSubmit.setOnClickListener {
            viewModel.projectList(
                province_id = mProvinceId,
                city_id = mCityId,
                title = etMainProjectSearchContent.text.toString()
            )
        }
    }

    override fun initData() {
        viewModel.projectList()
    }

    override fun initViewMode() {
        viewModel.projectDataLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe

            val p = data.find { it.id !=2 && it.id != 3 }
            if (p != null) {
                data.remove(p)
                data.add(data.size,p)
            }

            mProjectGroupAdapter.setNewInstance(data)
        })

        viewModel.regionListLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val provinceList = data.map { it.name }

            val cityList: MutableList<MutableList<String>> = mutableListOf()
            data.forEach { province ->
                if (province.children.isNullOrEmpty()) {
                    cityList.add(mutableListOf())
                } else {
                    cityList.add(province.children.map {
                        it.name
                    }.toMutableList())
                }
            }

            mOptionsRegion.setPicker(provinceList, cityList)
            mOptionsRegion.show()
        })
    }
}